package com.cy;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.cy.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.atomic.AtomicLong;

@EnableFeignClients
@SpringBootApplication
public class ConsumerRunApp {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerRunApp.class, args);
    }

    @Bean//这种写法一般做第三方资源整合的时候会用
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
    @Bean
    @LoadBalanced
    public RestTemplate loadBalancedRestTemplate(){
        return new RestTemplate();
    }

    @RestController
    public class ConsumerController{
        @Autowired
        private LoadBalancerClient loadBalancerClient;

        @Autowired
        private RestTemplate restTemplate;
        @Autowired
        private ConsumerService consumerService;

        @Autowired
        private RestTemplate loadBalancedRestTemplate;

        @Value("${spring.application.name}")
        private String appName;
        //构建一个线程安全并且可以实现自增自减操作的整数对象
        private AtomicLong aLong = new AtomicLong(1);

        @GetMapping("/consumer/echo")
        public String doRestEcho1(){
            consumerService.doGetResource();
            //调用服务提供方的API(http://ip:port/path)
            String url = "http://localhost:8081/provider/echo/"+appName;
            return restTemplate.getForObject(url, String.class);
        }
        @GetMapping("/consumer/echo2")
        public String doRestEcho2() throws InterruptedException {
            long num = aLong.getAndIncrement();
            if(num%2 == 0){
                Thread.sleep(200);//模拟耗时操作
            }/*系统设计时默认会认为响应时间超过200毫秒即慢调用*/
            ServiceInstance instance = loadBalancerClient.choose("sca-provider");
            String ip = instance.getHost();
            int port = instance.getPort();
            System.out.println(port);
            //String url = "http://"+ip+":"+port+"/provider/echo/"+appName;
            String url = String.format("http://%s:%s/provider/echo/%s",ip,port,appName);
            return restTemplate.getForObject(url, String.class);
        }
        @GetMapping("/consumer/echo3")
        public String doRestEcho(){
            consumerService.doGetResource();
            String url = String.format("http://sca-provider/provider/echo/%s", appName);
            return loadBalancedRestTemplate.getForObject(url,String.class);
        }

        @GetMapping("/consumer/fingById")
        @SentinelResource(value="res") //,blockHandler = ××.class，后面可以写异常处理机制的class
        public String doFindById(@RequestParam("id") Integer id){
            //@RequestParam("id")可写可不写，如果写了这个注解，id必须传值，否则会报错
            return "Resource id is "+id;
        }
    }
}
